package com.baibei.pay.withdraw;

import android.content.Context;

import com.baibei.basic.BasicPresenterImpl;
import com.baibei.ebec.sdk.ApiFactory;
import com.baibei.ebec.sdk.IPushApi;
import com.baibei.ebec.sdk.IUserApi;
import com.baibei.model.BalanceInfo;
import com.baibei.model.BankInfo;
import com.baibei.model.FundTimeInfo;
import com.baibei.model.ProvinceInfo;
import com.baibei.pay.selector.ProvinceHelper;
import com.baibei.sdk.ApiDefaultObserver;
import com.baibei.sdk.Empty;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * 提取现金表现层
 * Created by shenminjie on 2017/8/15.
 * mail:shenminjie@100bei.com
 */

public class WithdrawPresenterImpl extends BasicPresenterImpl<WithdrawContract.WithdrawView> implements WithdrawContract.Presenter {

    /**
     * 余额接口
     */
    private IUserApi mUserApi;

    /**
     * 验证码
     */
    private IPushApi mPushApi;

    /**
     * 银行列表
     */
    private List<BankInfo> mBankInfos;

    private List<ProvinceInfo> mProvinces;


    public WithdrawPresenterImpl(Context context, WithdrawContract.WithdrawView view) {
        super(context, view);
        mUserApi = ApiFactory.getInstance().getUserApi();
        mPushApi = ApiFactory.getInstance().getPushApi();
    }

    @Override
    public void getBalanceAndBankList() {
        //获取余额与银行列表
        mView.showLoading();
        createObservable(Observable.create(new ObservableOnSubscribe<List<ProvinceInfo>>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<List<ProvinceInfo>> e) throws Exception {
                e.onNext(ProvinceHelper.getCityInfos());
            }
        }).subscribeOn(Schedulers.io())).flatMap(new Function<List<ProvinceInfo>, Observable<List<BankInfo>>>() {
            @Override
            public Observable<List<BankInfo>> apply(@NonNull List<ProvinceInfo> provinceInfos) throws Exception {
                mProvinces = provinceInfos;
                return createObservable(mUserApi.getBankList());
            }
        }).flatMap(new Function<List<BankInfo>, Observable<BalanceInfo>>() {

            @Override
            public Observable<BalanceInfo> apply(@NonNull List<BankInfo> infos) throws Exception {
                mBankInfos = infos;
                mView.onLoadBankList(infos);
                return createObservable(mUserApi.getBalanceInfo());
            }
        }).flatMap(new Function<BalanceInfo, Observable<FundTimeInfo>>() {
            @Override
            public Observable<FundTimeInfo> apply(@NonNull BalanceInfo info) throws Exception {
                mView.onLoadBalanceInfo(info);
                return createObservable(mUserApi.getFundTimeInfo("OUT"));
            }
        }).subscribe(new ApiDefaultObserver<FundTimeInfo>() {
            @Override
            protected void onError(String s) {
                mView.hideLoading();
                mView.onLoadBasicDataFailed(s);
            }

            @Override
            protected void accept(FundTimeInfo info) {
                mView.hideLoading();
                mView.onLoadFundTimeInfo(info.getInfo());
            }
        });
    }

    @Override
    public void getCheckCode() {
//        createObservable(mPushApi.sendSMS(mView.getCheckCodeType(), mView.getPhone())).subscribe(new ApiDefaultObserver<Empty>() {
//            @Override
//            protected void onError(String s) {
//                mView.onSmsFailed(s);
//            }
//
//            @Override
//            protected void accept(Empty empty) {
//                mView.onSmsSuccess();
//            }
//        });
    }

    @Override
    public void withdrawAmount() {
        mView.showWithdrawLoading();
        createObservable(mUserApi.withdrawAmout(
                mView.getWithdrawAmount(),
                mView.getProvince(),
                mView.getCity(),
                mView.getBankCode(),
                mView.getSubBank(),
                mView.getBankAccount(),
                mView.getRealName(),
                mView.getFundPassword(),
                mView.getCheckCode()))
                .subscribe(new ApiDefaultObserver<Empty>() {
                    @Override
                    protected void onError(String s) {
                        mView.hideLoading();
                        mView.onWithdrawFailed(s);
                    }

                    @Override
                    protected void accept(Empty empty) {
                        mView.hideLoading();
                        mView.onWithdrawSuccess();
                    }
                });
    }

    @Override
    public List<BankInfo> getBankInfos() {
        return mBankInfos;
    }

    @Override
    public List<ProvinceInfo> getProvinceInfos() {
        return mProvinces;
    }
}
